package com.easyn.P2PCamLive.Deluxe;

import com.easyn.P2PCam264.AddDeviceActivity;
import com.easyn.CamLinePro.DeviceListActivity;
import com.easyn.CamLinePro.R;

import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class AddDeviceSelectActivity extends Activity {

	private ConnectivityManager manager;
	StringBuffer sb = new StringBuffer(256);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_add_device_select);
	}

	//判断是否wifi模式
	private boolean isNetworkAvailable() {
		boolean flag = false;
		// 得到网络连接信息
		manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		// 去进行判断网络是否连接
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}else{
			Toast.makeText(this,  getText(R.string.adddeviceselect_wifierror).toString(), Toast.LENGTH_SHORT).show();
			finish();
			return false;
		}
		
		//3G需要提示
		State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
			Toast.makeText(this,  getText(R.string.adddeviceselect_wifierror).toString(), Toast.LENGTH_SHORT).show();
			finish();
			return false;			
		} else {
			//sb.append("\ngprs is closed! ");
		}

		// 判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
		if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			//Toast.makeText(this, "wifi is open! ", Toast.LENGTH_SHORT).show();
			//sb.append("\nwifi is open! ");
			
			return true;//是wifi模式
		} else {
			//sb.append("\nwifi is closed! ");
		}		
		Toast.makeText(this,  getText(R.string.adddeviceselect_wifierror).toString(), Toast.LENGTH_SHORT).show();
		finish();
		return false;

	}

	 
	public void onAddNew(View v) {
		if (isNetworkAvailable()) {
			Intent Intent2 = new Intent();
			setResult(99, Intent2);
			finish();
			Intent intent = new Intent();
			intent.setClass(AddDeviceSelectActivity.this,
					AddDeviceSelectHint.class);
			startActivityForResult(intent, RESULT_OK);
		}
	}
	public void onAddOld(View v) {
		DeviceListActivity.gAddOld = true;
		finish();
	}
}
